package solved;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Stack;

/*
 ID:	liluyan1
 LANG:	JAVA
 TASK:	clocks
 */
public class clocks {
	public static void main(String[] args) throws IOException {
		BufferedReader f = new BufferedReader(new FileReader("clocks.in"));
		String res = new clocks().solve(f);
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
				"clocks.out")));
		out.println(res);
		out.close();
		System.exit(0);
	}

	private int[] mask = { 0x1b, 0x7, 0x36, 0x49, 0xba, 0x124, 0xd8, 0x1c0,
			0x1b0 };

	private String solve(BufferedReader f) throws IOException {
		int[][] init = new int[3][3];
		boolean[] visited = new boolean[1<<18+1];
		for (int i = 0; i < 3; i++) {
			init[i] = read(f, 3);
			for (int j = 0; j < 3; j++)
				init[i][j] = init[i][j] / 3 - 1;
		}

		int s = arrayToInt(init);
		visited[s] = true;

		ArrayDeque<State> queue = new ArrayDeque<State>();
		queue.add(new State(s, -1, null));

		int dest = 0x3ffff;

		while (!queue.isEmpty()) {
			State current = queue.remove();
			for (int i = 0; i < 9; i++) {
				int next = move(current.state, mask[i]);
				if (next == dest) {
					return answer(new State(next, i + 1, current));
				}
				if (!visited[next]) {
					queue.add(new State(next, i + 1, current));
					visited[next] = true;
				}
			}
		}

		return "";
	}

	private int move(int state, int m) {
		for (int i = 0; i < 9; i++) {
			if ((m & (1<<i)) == 0) continue;
				
			int b = (state >> (2*i)) & 3;
			b = (b + 1) % 4;
			state &= ~(3 << 2*i);
			state |= b << 2*i;
		}
		return state;
	}

	private String answer(State s) {
		Stack<Integer> stack = new Stack<Integer>();
		while (s.route >= 0) {
			stack.add(s.route);
			s = s.parent;
		}
		StringBuilder res = new StringBuilder();
		while (!stack.isEmpty()) {
			int t = stack.pop();
			res.append(" " + t);
		}
		return res.substring(1);
	}

	private int arrayToInt(int[][] a) {
		int s = 0;
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				s |= a[i][j] << (6 * i + 2 * j);
			}
		}
		return s;
	}

	private class State {
		int state;
		int route;
		State parent;

		public State(int state, int route, State parent) {
			this.state = state;
			this.route = route;
			this.parent = parent;
		}
	}

	public int[] read(BufferedReader f, int N) throws IOException {
		String[] t = f.readLine().split(" ");
		int[] a = new int[N];
		for (int i = 0; i < N; i++) {
			a[i] = Integer.parseInt(t[i]);
		}
		return a;
	}
}
